#include "lcm_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include <algorithm>
#include <vector>
constexpr uint32_t BLOCK_SIZE = 32;
namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
    LcmTilingData tiling;
    auto input_shape = context->GetInputShape(0)->GetStorageShape();
    auto other_shape = context->GetInputShape(1)->GetStorageShape();
    uint32_t inputNum = context->GetInputShape(0)->GetStorageShape().GetShapeSize(); 
    uint32_t inputBytes = GetSizeByDataType(context->GetInputDesc(0)->GetDataType()); 
    uint32_t inputLength = inputBytes * inputNum; 
    uint32_t otherNum = context->GetInputShape(1)->GetStorageShape().GetShapeSize(); 
    if(inputNum != otherNum)
    {
        LcmTilingDataBC tiling;
        uint32_t repeat = 1;
        uint32_t cycles = 1;
        uint32_t interval = 1;
        for(int i=0; i<input_shape.GetDimNum(); i++)
        {
            if(input_shape.GetDim(i) != other_shape.GetDim(i))
            {
                cycles = input_shape.GetDim(i);
                break;
            }
            repeat *= input_shape.GetDim(i);
        }
        interval = inputNum / cycles / repeat;
        if(40 % repeat == 0 && cycles*repeat > 40)
        {
            uint32_t block = 40/repeat;
            uint32_t BLOCK_DIM = repeat;
            BLOCK_DIM = 40;
            uint32_t bigNum = cycles % block;
            uint32_t bigLength = cycles / block;
            uint32_t smallLength = cycles / block;
            if(bigNum != 0) bigLength ++;
            tiling.set_bigNum(bigNum);
            tiling.set_bigLength(bigLength);
            tiling.set_smallLength(smallLength);
            tiling.set_repeat(block);
            tiling.set_cycles(cycles);
            tiling.set_interval(interval);
            context->SetTilingKey(3);
            context->SetBlockDim(BLOCK_DIM);
        }
        else
        {
            uint32_t BLOCK_DIM = repeat;
            if(BLOCK_DIM > 40)
                BLOCK_DIM = 40;
            uint32_t bigNum = repeat % BLOCK_DIM;
            uint32_t bigLength = repeat / BLOCK_DIM;
            uint32_t smallLength = repeat / BLOCK_DIM;
            if(bigNum != 0) bigLength ++;
            tiling.set_bigNum(bigNum);
            tiling.set_bigLength(bigLength);
            tiling.set_smallLength(smallLength);
            tiling.set_repeat(repeat);
            tiling.set_cycles(cycles);
            tiling.set_interval(interval);
            context->SetTilingKey(2);
            context->SetBlockDim(BLOCK_DIM);
        } 
        tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
        context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    }
    else
    {
        uint32_t BLOCK_DIM = inputLength/64;
        if(BLOCK_DIM > 40)
            BLOCK_DIM = 40;
        uint32_t ALIGN_NUM = BLOCK_SIZE / inputBytes;
        uint32_t totalLengthAligned = ((inputNum + ALIGN_NUM - 1) / ALIGN_NUM) * ALIGN_NUM;
        uint32_t bigNum = (totalLengthAligned / ALIGN_NUM) % BLOCK_DIM;
        uint32_t bigLength = ((totalLengthAligned / BLOCK_DIM + ALIGN_NUM - 1) / ALIGN_NUM) * ALIGN_NUM;
        uint32_t smallLength = (totalLengthAligned / BLOCK_DIM / ALIGN_NUM) * ALIGN_NUM;
        tiling.set_bigNum(bigNum);
        tiling.set_bigLength(bigLength);
        tiling.set_smallLength(smallLength);
        context->SetTilingKey(1);
        context->SetBlockDim(BLOCK_DIM);
        tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
        context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    }
    return ge::GRAPH_SUCCESS;
}
}
namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
static ge::graphStatus InferDataType(gert::InferDataTypeContext *context)
{
const auto inputDataType = context->GetInputDataType(0);
context->SetOutputDataType(0, inputDataType);
return ge::GRAPH_SUCCESS;
}
}
namespace ops {
class Lcm : public OpDef {
public:
    explicit Lcm(const char* name) : OpDef(name)
    {
        this->Input("input")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT8, ge::DT_INT16, ge::DT_INT32, ge::DT_INT64})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("other")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT8, ge::DT_INT16, ge::DT_INT32, ge::DT_INT64})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("out")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT8, ge::DT_INT16, ge::DT_INT32, ge::DT_INT64})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->SetInferShape(ge::InferShape).SetInferDataType(ge::InferDataType);
        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910b");
    }
};
OP_ADD(Lcm);
}
